import { ref } from 'vue'
import { useRouter } from 'vue-router'
import testResult from "@/api/questionItem/testResult"

export function useTestAnalysis() {
  const router = useRouter()
  const dialogVisible = ref(false)
  const testInfo = ref({})

  // 日期格式化函数
  const formatDate = (dateStr) => {
    const date = new Date(dateStr)
    return date.toLocaleString("zh-CN", {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit",
    })
  }

  // 查看试卷处理函数
  const handleViewPaper = (data) => {
    router.push({
      path: "/test/answer",
      query: {
        questionItemId: data.questionItemId,
        account: data.account,
      },
    })
  }

  // 图表点击事件处理函数
  const handleChartClick = async (params) => {
    const id = params.data.id
    const res = await testResult.getById(id)
    testInfo.value = res.data
    dialogVisible.value = true
  }

  // 通用的图表配置生成函数
  const generateChartOption = (xaxisData, yaxisData, ids) => {
    return {
      xAxis: {
        type: "category",
        data: xaxisData,
      },
      yAxis: {
        type: "value",
      },
      series: [
        {
          data: yaxisData.map((value, idx) => ({
            value,
            id: ids[idx],
          })),
          type: "line",
          textStyle: {
            fontSize: 14,
          },
          label: { show: true, position: "top" },
          emphasis: {
            itemStyle: {
              color: "red",
            },
          },
        },
      ],
    }
  }

  return {
    dialogVisible,
    testInfo,
    formatDate,
    handleViewPaper,
    handleChartClick,
    generateChartOption
  }
} 